package com.demo.viewtobitmap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.myalldemo.R;

public class MainViewToBitmapActivity extends Activity {
	ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewtobitmap_main);
		iv = (ImageView) findViewById(R.id.iv1);
	}

	public void btnclick(View v) {
		switch (v.getId()) {
		case R.id.btn:
			/**
			 * 以下代码包含了view转bitmap的方法
			 * 注意View view = this.getWindow().getDecorView();这句话不能在在onCreat或者onResume中用
			 * 不然获得的view为空
			 * 只有在当前view创建完成之后才能使用
			 */
			// View是你需要截图的View
			View view = this.getWindow().getDecorView();
			view.setDrawingCacheEnabled(true);
			view.buildDrawingCache();
			Bitmap b1 = view.getDrawingCache();

			// 获取状态栏高度
			Rect frame = new Rect();
			this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
			int statusBarHeight = frame.top;
			Log.i("TAG", "" + statusBarHeight);

			// 获取屏幕长和高
			int width = this.getWindowManager().getDefaultDisplay().getWidth();
			int height = this.getWindowManager().getDefaultDisplay()
					.getHeight();
			// 去掉标题栏
			// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
			Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width,
					height - statusBarHeight);
			view.destroyDrawingCache();
			iv.setImageBitmap(b);
			break;

		default:
			break;
		}
	}

}
